package com.example.take_out_dish.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.take_out_dish.common.Result;
import com.example.take_out_dish.common.lang.dto.DishDto;
import com.example.take_out_dish.common.lang.vo.BaseVo;
import com.example.take_out_dish.common.lang.vo.DishVo;
import com.example.take_out_dish.entity.Dish;
import com.example.take_out_dish.service.DishService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品管理 前端控制器
 * </p>
 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Resource
    private DishService dishService;

    @PostMapping
    public Result save(@RequestBody DishDto dishDto) {
        dishService.saveWithFlavor(dishDto);
        return Result.success();
    }

    @PostMapping("/page")
    public Result<Page> page(@RequestBody BaseVo baseVo) {
        Page pageInfo = dishService.page(new Page(baseVo.getPage(), baseVo.getPageSize()));
        log.info("pageInfo:{}", pageInfo);
        return Result.success(pageInfo);
    }

    /**
     * 菜品批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestBody List<String> ids) {
        dishService.delete(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<DishVo> getById(@PathVariable("id") String id) {
        return Result.success(dishService.getByIdWithFlavor(id));
    }

    @PutMapping
    public Result update(@RequestBody DishDto dishDto) {
        dishService.update(dishDto);
        return Result.success();
    }

    @PostMapping("/status/{status}/{id}")
    public Result updateStatus(@PathVariable("status") Integer status,
                               @PathVariable("id") String id) {
        dishService.updateStatus(status, id);
        return Result.success();
    }


    @GetMapping("/list")
    public Result<List<Dish>> list(String categoryId) {
        List<Dish> list = dishService.selectByCategoryId(categoryId);
        return Result.success(list);
    }
}
